﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;

namespace _1042009_1042034
{
    public partial class uctLapPhieuThue : UserControl
    {
        public uctLapPhieuThue()
        {
            InitializeComponent();
        }


        List<PhongDTO> lst = new List<PhongDTO>();

        private void uctLapPhieuThue_Load(object sender, EventArgs e)
        {
            int MaPhieuThue = Business.PhieuThueService.GetIdentity();
            this.txtMaPhieuThue.Text = (MaPhieuThue + 1).ToString();
            this.cbbLoaiPhong.Load();
            this.cbbLoaiPhong.SelectedItem = null;

            int SoKhachToiDa = Business.ThamSoService.SoKhachToiDa();
            this.lblGhiChu.Text = "(*) Đơn giá chỉ áp dụng cho phòng có từ " + SoKhachToiDa.ToString() + " khách trở xuống và không có người nước ngoài.";
           
            this.lst = Business.PhongService.LayDanhSach();
            this.lst = Business.PhongService.LayDanhSachPhongChuaThueTuList(lst);
            this.SetDataComboBoxPhong(lst);

            this.txtNgayThue.Text = this.dateTimePickerNgatyThue.Value.ToString();
        }


        void SetDataComboBoxPhong(List<PhongDTO> lstPhong)
        {
            this.cbbPhong.Text = "";
            this.cbbPhong.DataSource = lstPhong;
            this.cbbPhong.DisplayMember = PhongDTO.DisplayMember;
            this.cbbPhong.ValueMember = PhongDTO.ValueMember;
        } 

        private void cbbLoaiPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int MaloaiPhong = int.Parse(this.cbbLoaiPhong.SelectedValue.ToString());
                List<PhongDTO> lstPhongChuaThue = Business.PhongService.LayDanhSachPhongChuaThueTuList(this.lst, MaloaiPhong);
                this.SetDataComboBoxPhong(lstPhongChuaThue);

                LoaiPhongDTO Lp = (LoaiPhongDTO)this.cbbLoaiPhong.SelectedItem;
                this.txtDonGia.Text = Lp.DonGia.ToString();
            }
            catch { }
        }


        private bool KiemTraThongTinPhong(ref string Mes)
        {
            bool kt = true;
            if (this.cbbPhong.Items.Count == 0)
            {
                Mes = "Chưa chọn phòng";
                return false;
            }
            return kt;
        }
      
        private void btnTaoPhieuThue_Click(object sender, EventArgs e)
        {
            //kiem tra thong tin phong thue
            string Mes = "";
            bool kt = this.KiemTraThongTinPhong(ref Mes);
            if ( kt== false)
            {
                MessageBox.Show(Mes);
                return;
            }


            //kiem tra quy dinh o day 
            int SoKhachToiDa = Business.ThamSoService.SoKhachToiDa();
            List<KhachHangDTO> LstKhach = this.uctDanhSachKhachThue.LayDanhSachKhachThue();
            if (LstKhach.Count == 0 || LstKhach.Count > SoKhachToiDa)
            {
                MessageBox.Show("Số lượng ( dữ liệu) Khách hàng không hợp lệ");
                return;
            }

            //them khach hang vao gio hang
            for (int i = 0; i < LstKhach.Count; i++)
            {
                Business.KhachHangService.Them(LstKhach[i]);
            }

            //khi du lieu da day du

            PhieuThueDTO PhieuThue = new PhieuThueDTO();
            double DonGiaPhong = double.Parse(this.txtDonGia.Text);

            double TiLePhuTHuKhachNuocNgoai = Business.ThamSoService.PhuThuKhachNuocNgoai(LstKhach);
            double TiLePhuTHuSoLuongKhach = Business.ThamSoService.PhuThuSoLuongKhach(LstKhach);

            PhieuThue.DonGia = (DonGiaPhong * TiLePhuTHuKhachNuocNgoai) + (DonGiaPhong * TiLePhuTHuSoLuongKhach);
            PhieuThue.MaPhieuThue = int.Parse(this.txtMaPhieuThue.Text);

            PhongDTO p = (PhongDTO)this.cbbPhong.SelectedItem;
            p.MaTinhTrang = TinhTrangDTO.MaDaThue;

            Business.PhongService.CapNhat(p);

            
            PhieuThue.MaPhong = p.MaPhong;
            PhieuThue.NgayBatDauThue = this.dateTimePickerNgatyThue.Value;

            PhieuThue.NgayKetThuc = this.dateTimePickerNgatyThue.Value;

            Business.PhieuThueService.Them(PhieuThue);

            //them chi tiet phieu thue
            ChiTietPhieuThueDTO ct = new ChiTietPhieuThueDTO();
            for (int i = 0; i < LstKhach.Count; i++)
            {
                ct.MaPhieuThue = PhieuThue.MaPhieuThue;
                ct.MaKhachHang = LstKhach[i].MaKhachHang;

                Business.ChiTietPhieuThueService.Them(ct);
            }


            MessageBox.Show("Đặt phòng thành công");

        }

        private void cbbPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PhongDTO phong = (PhongDTO)this.cbbPhong.SelectedItem;
                LoaiPhongDTO Lp = Business.LoaiPhongService.LayTheoMa(phong.MaLoaiPhong);
                this.txtDonGia.Text = Lp.DonGia.ToString();
            }
            catch { }
        }

    }
}
